<html>
<head><meta charset="utf-8"><title>Serde zero-copy question · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Serde.20zero-copy.20question.html">Serde zero-copy question</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="199095732"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Serde%20zero-copy%20question/near/199095732" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Frank McSherry <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Serde.20zero-copy.20question.html#199095732">(May 28 2020 at 22:49)</a>:</h4>
<p>Hi folks. Sorry if this isn't the right spot, but it's where I know folks are.</p>
<p>I have a <code>serde</code> question, trying to get zero-copy deserialization up and running. I've whipped together </p>
<div class="codehilite"><pre><span></span><code><span class="k">struct</span> <span class="nc">CommandVec</span><span class="o">&lt;</span><span class="na">&#39;de</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">buffer</span>: <span class="nb">Vec</span><span class="o">&lt;</span><span class="n">Command</span><span class="o">&lt;</span><span class="na">&#39;de</span><span class="o">&gt;&gt;</span><span class="p">,</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="cp">#[derive(Debug)]</span><span class="w"></span>
<span class="k">enum</span> <span class="nc">Command</span><span class="o">&lt;</span><span class="na">&#39;de</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">Null</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="n">Bool</span><span class="p">(</span><span class="kt">bool</span><span class="p">),</span><span class="w"></span>
<span class="w">    </span><span class="n">Number</span><span class="p">(</span><span class="kt">f64</span><span class="p">),</span><span class="w"></span>
<span class="w">    </span><span class="nb">String</span><span class="p">(</span><span class="o">&amp;</span><span class="na">&#39;de</span><span class="w"> </span><span class="kt">str</span><span class="p">),</span><span class="w"></span>
<span class="w">    </span><span class="n">ArrayStart</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="n">ArrayEnd</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="n">MapStart</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="n">MapEnd</span><span class="p">,</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>


<p>and would very much like to deserialize a <code>CommandVec&lt;'de&gt;</code>. However, it seems like the <code>Visitor</code> trait needs to return owned instances of the deserializing type, and I can't think of a way to stash a <code>&amp;mut CommandVec&lt;'de&gt;</code> anywhere (the <code>deserialize</code> method returns <code>Self</code> and is repeatedly invoked to deserialize an element from scratch). </p>
<p>Is anyone familiar with having done this before? I'm delighted to not have to copy strings around, but an allocation for each node in the parse tree is not ideal.</p>



<a name="199096137"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Serde%20zero-copy%20question/near/199096137" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> David Tolnay <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Serde.20zero-copy.20question.html#199096137">(May 28 2020 at 22:54)</a>:</h4>
<p>I don't quite follow the setup; is CommandVec the input or the output of the deserialization step?</p>



<a name="199096172"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Serde%20zero-copy%20question/near/199096172" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Frank McSherry <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Serde.20zero-copy.20question.html#199096172">(May 28 2020 at 22:54)</a>:</h4>
<p>Sorry. It's intended to be the output, for example something like</p>
<div class="codehilite"><pre><span></span><code><span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">foo</span>: <span class="nc">CommandVec</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">serde_json</span>::<span class="n">from_str</span>::<span class="o">&lt;</span><span class="n">CommandVec</span><span class="o">&gt;</span><span class="p">(</span><span class="n">text</span><span class="p">).</span><span class="n">expect</span><span class="p">(</span><span class="s">&quot;???&quot;</span><span class="p">);</span><span class="w"></span>
</code></pre></div>



<a name="199096207"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Serde%20zero-copy%20question/near/199096207" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Frank McSherry <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Serde.20zero-copy.20question.html#199096207">(May 28 2020 at 22:55)</a>:</h4>
<p>The goal would be to only ever have one allocation, ideally something I could provide actually, and populate it with a sequence of <code>Command&lt;'de&gt;</code> just by having the visitor traverse the parse tree.</p>



<a name="199096231"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Serde%20zero-copy%20question/near/199096231" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Frank McSherry <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Serde.20zero-copy.20question.html#199096231">(May 28 2020 at 22:55)</a>:</h4>
<p>Maybe I'm barking up the wrong tree and some <code>serde-json</code> internals are what I want to use, but I did want to avoid re-implementing JSON parsing if I can.</p>



<a name="199096341"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Serde%20zero-copy%20question/near/199096341" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> David Tolnay <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Serde.20zero-copy.20question.html#199096341">(May 28 2020 at 22:57)</a>:</h4>
<p>this is something you would do by writing a Deserialize&lt;'de&gt; impl for CommandVec&lt;'de&gt;</p>



<a name="199096358"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Serde%20zero-copy%20question/near/199096358" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> David Tolnay <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Serde.20zero-copy.20question.html#199096358">(May 28 2020 at 22:57)</a>:</h4>
<p>there shouldn't need to be serde_json internals involved</p>



<a name="199096368"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Serde%20zero-copy%20question/near/199096368" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> David Tolnay <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Serde.20zero-copy.20question.html#199096368">(May 28 2020 at 22:57)</a>:</h4>
<p>do you have a playground of what you tried?</p>



<a name="199096389"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Serde%20zero-copy%20question/near/199096389" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Frank McSherry <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Serde.20zero-copy.20question.html#199096389">(May 28 2020 at 22:57)</a>:</h4>
<p>I'll slap it up. I did write a <code>Deserialize&lt;'de&gt;</code> but .. it does more allocations than I would like.</p>



<a name="199096452"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Serde%20zero-copy%20question/near/199096452" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Frank McSherry <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Serde.20zero-copy.20question.html#199096452">(May 28 2020 at 22:58)</a>:</h4>
<p><a href="https://gist.github.com/frankmcsherry/40af1f932f15cc738cebd90d622a6fec">https://gist.github.com/frankmcsherry/40af1f932f15cc738cebd90d622a6fec</a></p>



<a name="199096490"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Serde%20zero-copy%20question/near/199096490" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> David Tolnay <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Serde.20zero-copy.20question.html#199096490">(May 28 2020 at 22:58)</a>:</h4>
<p>I would expect that the Deserialize impl for CommandVec would create a Vec&lt;Command&gt; and create a Visitor containing a mut reference to that Vec</p>



<a name="199096503"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Serde%20zero-copy%20question/near/199096503" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Frank McSherry <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Serde.20zero-copy.20question.html#199096503">(May 28 2020 at 22:59)</a>:</h4>
<p><a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=a16971c3d235a4542c5effbe3eeec67c">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=a16971c3d235a4542c5effbe3eeec67c</a></p>



<a name="199096534"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Serde%20zero-copy%20question/near/199096534" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> David Tolnay <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Serde.20zero-copy.20question.html#199096534">(May 28 2020 at 22:59)</a>:</h4>
<p>then when the visitor finishes the mut borrow on the vec ends and the original vec can be moved into the field of the returned CommandVec</p>



<a name="199096624"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Serde%20zero-copy%20question/near/199096624" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Frank McSherry <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Serde.20zero-copy.20question.html#199096624">(May 28 2020 at 23:00)</a>:</h4>
<p>Right, but afaict each recursive call invokes <code>&lt;CommandVec as Deserialize&gt;::deserialize(...)</code> from scratch.</p>



<a name="199096647"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Serde%20zero-copy%20question/near/199096647" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> David Tolnay <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Serde.20zero-copy.20question.html#199096647">(May 28 2020 at 23:00)</a>:</h4>
<p>which line?</p>



<a name="199096693"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Serde%20zero-copy%20question/near/199096693" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Frank McSherry <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Serde.20zero-copy.20question.html#199096693">(May 28 2020 at 23:01)</a>:</h4>
<p>The recursive calls <code>serde</code> implements. If you put a </p>
<div class="codehilite"><pre><span></span><code><span class="n">println</span><span class="o">!</span><span class="p">(</span><span class="s">&quot;heya&quot;</span><span class="p">);</span><span class="w"></span>
<span class="n">deserializer</span><span class="p">.</span><span class="n">deserialize_any</span><span class="p">(</span><span class="n">ValueVisitor</span><span class="p">)</span><span class="w"></span>
</code></pre></div>


<p>in to see what gets called, you get large numbers of <code>heya</code> in the output.</p>



<a name="199096774"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Serde%20zero-copy%20question/near/199096774" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Frank McSherry <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Serde.20zero-copy.20question.html#199096774">(May 28 2020 at 23:02)</a>:</h4>
<p>Sorry, I apologize if this sounds a bit random; I have a bunch of built up context and should go slower and try to be clearer.</p>



<a name="199096800"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Serde%20zero-copy%20question/near/199096800" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> David Tolnay <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Serde.20zero-copy.20question.html#199096800">(May 28 2020 at 23:02)</a>:</h4>
<p>oh the visit_seq is calling visitor.next_element::&lt;CommandVec&gt;()</p>



<a name="199096808"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Serde%20zero-copy%20question/near/199096808" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> David Tolnay <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Serde.20zero-copy.20question.html#199096808">(May 28 2020 at 23:02)</a>:</h4>
<p>which is the Deserialize impl for CommandVec</p>



<a name="199096839"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Serde%20zero-copy%20question/near/199096839" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Frank McSherry <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Serde.20zero-copy.20question.html#199096839">(May 28 2020 at 23:03)</a>:</h4>
<p>Does it have a better option?</p>



<a name="199096844"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Serde%20zero-copy%20question/near/199096844" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> David Tolnay <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Serde.20zero-copy.20question.html#199096844">(May 28 2020 at 23:03)</a>:</h4>
<p>what I would expect to see is <code>visitor.next_element_seed(CommandVecCollector { vec: self.vec })</code></p>



<a name="199096860"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Serde%20zero-copy%20question/near/199096860" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Frank McSherry <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Serde.20zero-copy.20question.html#199096860">(May 28 2020 at 23:03)</a>:</h4>
<p>eek, ok that is good information.</p>



<a name="199096865"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Serde%20zero-copy%20question/near/199096865" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> David Tolnay <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Serde.20zero-copy.20question.html#199096865">(May 28 2020 at 23:03)</a>:</h4>
<p>where CommandVecCollector is your type that implements Visitor</p>



<a name="199096883"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Serde%20zero-copy%20question/near/199096883" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> David Tolnay <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Serde.20zero-copy.20question.html#199096883">(May 28 2020 at 23:03)</a>:</h4>
<p>basically <code>struct CommandVecCollector&lt;'a, 'de&gt; { vec: &amp;'a mut Vec&lt;Command&lt;'de&gt;&gt; }</code></p>



<a name="199096973"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Serde%20zero-copy%20question/near/199096973" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Frank McSherry <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Serde.20zero-copy.20question.html#199096973">(May 28 2020 at 23:04)</a>:</h4>
<p>Gotcha. That is super-helpful. I'll be back to report success or failure!</p>



<a name="199096975"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Serde%20zero-copy%20question/near/199096975" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> David Tolnay <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Serde.20zero-copy.20question.html#199096975">(May 28 2020 at 23:04)</a>:</h4>
<p>with <code>impl&lt;'a, 'de&gt; Visitor&lt;'de&gt; for CommandVecCollector&lt;'a, 'de&gt;</code> being the Visitor impl you write</p>



<a name="199097167"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Serde%20zero-copy%20question/near/199097167" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Frank McSherry <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Serde.20zero-copy.20question.html#199097167">(May 28 2020 at 23:07)</a>:</h4>
<blockquote>
<p>DeserializeSeed is the stateful form of the Deserialize trait. If you ever find yourself looking for a way to pass data into a Deserialize impl, this trait is the way to do it.</p>
</blockquote>
<p>Ah yes, this is almost surely what I want.</p>



<a name="199097289"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Serde%20zero-copy%20question/near/199097289" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> David Tolnay <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Serde.20zero-copy.20question.html#199097289">(May 28 2020 at 23:08)</a>:</h4>
<p>that's not clear to me, unless you want to reuse the same vec across multiple different CommandVec deserializations</p>



<a name="199097306"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Serde%20zero-copy%20question/near/199097306" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> David Tolnay <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Serde.20zero-copy.20question.html#199097306">(May 28 2020 at 23:08)</a>:</h4>
<p>if each time you deserialize a CommandVec you make a new Vec then there is no data going into that deserialize impl</p>



<a name="199097395"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Serde%20zero-copy%20question/near/199097395" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Frank McSherry <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Serde.20zero-copy.20question.html#199097395">(May 28 2020 at 23:10)</a>:</h4>
<p>I do feel like I want to inject state in to the recursive calls. And <code>next_element_seed</code> requires an implementor of <code>DeserializeSeed</code>.</p>



<a name="199097568"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Serde%20zero-copy%20question/near/199097568" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Frank McSherry <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Serde.20zero-copy.20question.html#199097568">(May 28 2020 at 23:13)</a>:</h4>
<p>Though you probably know better than I do (I've been looking at this for an hour) and the traits have me tied up in knots.</p>



<a name="199097756"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Serde%20zero-copy%20question/near/199097756" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> David Tolnay <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Serde.20zero-copy.20question.html#199097756">(May 28 2020 at 23:16)</a>:</h4>
<p><a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=7ece0a083058c394e435f8d087117267">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=7ece0a083058c394e435f8d087117267</a></p>



<a name="199097762"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Serde%20zero-copy%20question/near/199097762" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> David Tolnay <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Serde.20zero-copy.20question.html#199097762">(May 28 2020 at 23:16)</a>:</h4>
<p>what i had in mind</p>



<a name="199097842"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Serde%20zero-copy%20question/near/199097842" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Frank McSherry <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Serde.20zero-copy.20question.html#199097842">(May 28 2020 at 23:18)</a>:</h4>
<p>Ok, that looks very sane!</p>



<a name="199097876"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Serde%20zero-copy%20question/near/199097876" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Frank McSherry <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Serde.20zero-copy.20question.html#199097876">(May 28 2020 at 23:19)</a>:</h4>
<p>Do you know if it is easily done to trigger <code>DeserializeSeed</code> from something like <code>serde_json::from_str</code>? ( as in, if I do have a <code>&amp;mut Vec&lt;Command&lt;'de&gt;&gt;</code> already)</p>



<a name="199097882"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Serde%20zero-copy%20question/near/199097882" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Frank McSherry <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Serde.20zero-copy.20question.html#199097882">(May 28 2020 at 23:19)</a>:</h4>
<p>(( thank you very much, by the way! ))</p>



<a name="199097953"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Serde%20zero-copy%20question/near/199097953" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> David Tolnay <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Serde.20zero-copy.20question.html#199097953">(May 28 2020 at 23:20)</a>:</h4>
<p>for what purpose? to skip the CommandVec wrapper type?</p>



<a name="199097968"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Serde%20zero-copy%20question/near/199097968" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> David Tolnay <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Serde.20zero-copy.20question.html#199097968">(May 28 2020 at 23:21)</a>:</h4>
<p>ah i see your edit</p>



<a name="199097989"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Serde%20zero-copy%20question/near/199097989" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> David Tolnay <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Serde.20zero-copy.20question.html#199097989">(May 28 2020 at 23:21)</a>:</h4>
<p>yes you can call it directly</p>



<a name="199097993"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Serde%20zero-copy%20question/near/199097993" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Frank McSherry <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Serde.20zero-copy.20question.html#199097993">(May 28 2020 at 23:21)</a>:</h4>
<p>Sorry, was trying to be less spammy, not sneaky. :)</p>



<a name="199097999"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Serde%20zero-copy%20question/near/199097999" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> David Tolnay <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Serde.20zero-copy.20question.html#199097999">(May 28 2020 at 23:22)</a>:</h4>
<p>the arguments to DeserializeSeed::deserialize are the input object and the deserializer</p>



<a name="199098038"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Serde%20zero-copy%20question/near/199098038" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> David Tolnay <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Serde.20zero-copy.20question.html#199098038">(May 28 2020 at 23:22)</a>:</h4>
<p>so e.g.:</p>



<a name="199098058"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Serde%20zero-copy%20question/near/199098058" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> David Tolnay <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Serde.20zero-copy.20question.html#199098058">(May 28 2020 at 23:22)</a>:</h4>
<p><code>some_state.deserialize(&amp;mut serde_json::Deserializer::from_str(json_str))?;</code></p>



<a name="199098079"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Serde%20zero-copy%20question/near/199098079" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Frank McSherry <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Serde.20zero-copy.20question.html#199098079">(May 28 2020 at 23:23)</a>:</h4>
<p>!!! neat, thanks!</p>



<a name="199098377"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Serde%20zero-copy%20question/near/199098377" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Frank McSherry <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Serde.20zero-copy.20question.html#199098377">(May 28 2020 at 23:28)</a>:</h4>
<p>(( looks like about a 7x speed-up over <code>serde_json::from_str::&lt;Value&gt;()</code>, fwiw ))</p>



<a name="199098593"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Serde%20zero-copy%20question/near/199098593" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> David Tolnay <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Serde.20zero-copy.20question.html#199098593">(May 28 2020 at 23:32)</a>:</h4>
<p>good stuff</p>



<a name="199098597"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Serde%20zero-copy%20question/near/199098597" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> David Tolnay <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Serde.20zero-copy.20question.html#199098597">(May 28 2020 at 23:32)</a>:</h4>
<p>be aware that if the input has a string with escape sequences you won't be able to deserialize it to &amp;'de str</p>



<a name="199098694"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Serde%20zero-copy%20question/near/199098694" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> David Tolnay <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Serde.20zero-copy.20question.html#199098694">(May 28 2020 at 23:34)</a>:</h4>
<p>if you care about that, either add another variant for String, or see if you can get <a href="https://github.com/pickfire/json">https://github.com/pickfire/json</a> working</p>



<a name="199098697"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Serde%20zero-copy%20question/near/199098697" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> David Tolnay <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Serde.20zero-copy.20question.html#199098697">(May 28 2020 at 23:34)</a>:</h4>
<p>that fork has an entry point that takes &amp;mut str for the input json</p>



<a name="199098702"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Serde%20zero-copy%20question/near/199098702" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> David Tolnay <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Serde.20zero-copy.20question.html#199098702">(May 28 2020 at 23:34)</a>:</h4>
<p>and rewrites escape sequences in place</p>



<a name="199098810"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Serde%20zero-copy%20question/near/199098810" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Frank McSherry <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Serde.20zero-copy.20question.html#199098810">(May 28 2020 at 23:36)</a>:</h4>
<p>Oh wait can you say that part again? If there is a string with escape sequences, ... how does it present in the visitor? As <code>bytes</code> or as <code>String</code> or what?</p>



<a name="199098862"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Serde%20zero-copy%20question/near/199098862" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> David Tolnay <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Serde.20zero-copy.20question.html#199098862">(May 28 2020 at 23:37)</a>:</h4>
<p>i think serde_json uses visit_str</p>



<a name="199098865"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Serde%20zero-copy%20question/near/199098865" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> David Tolnay <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Serde.20zero-copy.20question.html#199098865">(May 28 2020 at 23:37)</a>:</h4>
<p>not 100% sure</p>



<a name="199098960"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Serde%20zero-copy%20question/near/199098960" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Frank McSherry <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Serde.20zero-copy.20question.html#199098960">(May 28 2020 at 23:38)</a>:</h4>
<p>Ok, I'll make a note to check it out though, thanks!</p>



<a name="199199156"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Serde%20zero-copy%20question/near/199199156" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Odd-Harald Myhren <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Serde.20zero-copy.20question.html#199199156">(May 29 2020 at 19:16)</a>:</h4>
<p>heya, im a decent rustacean, been at it for about half a year now with plenty of projects finished. is there a place for me to start when it comes to contributing to <code>rust-analyzer</code>?</p>



<a name="199244123"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Serde%20zero-copy%20question/near/199244123" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Laurențiu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Serde.20zero-copy.20question.html#199244123">(May 30 2020 at 07:40)</a>:</h4>
<p><span class="user-mention" data-user-id="307504">@Odd-Harald Myhren</span> see <a href="#narrow/stream/185405-t-compiler.2Fwg-rls-2.2E0">https://rust-lang.zulipchat.com/#narrow/stream/185405-t-compiler.2Fwg-rls-2.2E0</a> for <code>rust-analyzer</code> questions. You could start by looking at <a href="https://github.com/rust-analyzer/rust-analyzer/issues?q=is%3Aopen+is%3Aissue+label%3AE-has-instructions">https://github.com/rust-analyzer/rust-analyzer/issues?q=is%3Aopen+is%3Aissue+label%3AE-has-instructions</a>.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>